<?php
header("Content-Type: text/html; charset=iso-8859-1");
session_start();
@$ftmp = $_FILES['image']['tmp_name'];
@$oname = $_FILES['image']['name'];
@$fsize = $_FILES['image']['size'];
@$ftype = $_FILES['image']['type'];
$dir = "photos";
$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : 0;
if(!$id){
	$id = (!empty($_SESSION['tmp_id'])) ? $_SESSION['tmp_id'] : rand();	
	$_SESSION['tmp_id'] = $id;
} 

$types = array(
	1=>"gif",
	2=>"jpeg",
	3=>"png",
	4=>"swf",
	5=>"psd",
	6=>"bmp"
	);
if(IsSet($ftmp) && $id) :
	$type = $types[exif_imagetype($ftmp)];
	@$fname = $id.".".($types[exif_imagetype($ftmp)])."_tmp";
	$newname = $id.".jpeg";
	if(move_uploaded_file($ftmp,"$dir/".$fname)){
		$filename = $fname;
		list($width, $height) = getimagesize("$dir/".$filename);
		if($width < 100){
			rename("$dir/".$fname,"$dir/".$newname);
		}
		else{
			$new_width = 100;
			$new_height = 100;
			if($width > $height){
				$int_height = 100;
				$int_width = $width * 100 / $height;
			}
			else{
				$int_width = 100;
				$int_height = $height * 100 / $width;				
			}

			$cropStartX = ($int_width > $new_width) ? ($int_width - $new_width) / 2 : 0;
			$cropStartY = ($int_height > $new_height) ? ($int_height - $new_height) / 2 : 0;
			$image_s = imagecreatetruecolor($int_width, $int_height);
			$image_p = imagecreatetruecolor($new_width, $new_height);
			if($type == "jpeg"){
				$image = imagecreatefromjpeg("$dir/".$filename);
				imagecopyresampled($image_s, $image, 0, 0, 0, 0, $int_width, $int_height, $width, $height);
				imagecopyresized($image_p, $image_s,  0, 0, $cropStartX,$cropStartY, ($new_width + $cropStartX), ($new_height + $cropStartY), $int_width, $int_height);
				imagejpeg($image_p,"$dir/".$newname, 100);
				unlink("$dir/".$fname);
			}
			elseif($type == "gif"){
				$image = imagecreatefromgif("$dir/".$filename);
				imagecopyresampled($image_s, $image, 0, 0, 0, 0, $int_width, $int_height, $width, $height);
				imagecopyresized($image_p, $image_s,  0, 0, $cropStartX,$cropStartY, ($new_width + $cropStartX), ($new_height + $cropStartY), $int_width, $int_height);
				imagejpeg($image_p,"$dir/".$newname, 100);
				unlink("$dir/".$fname);				
			}
			elseif($type == "png"){
				$image = imagecreatefrompng("$dir/".$filename);
				imagecopyresampled($image_s, $image, 0, 0, 0, 0, $int_width, $int_height, $width, $height);
				imagecopyresized($image_p, $image_s,  0, 0, $cropStartX,$cropStartY, ($new_width + $cropStartX), ($new_height + $cropStartY), $int_width, $int_height);
				imagejpeg($image_p,"$dir/".$newname, 100);
				unlink("$dir/".$fname);				
			}
		}
		$fname = $newname;
		chmod($dir."/".$newname,0644);
	}
	$tag = $id;

	echo "<html><head>\n";
	echo "<script type = 'text/javascript'>\n";
	echo "var par = window.parent.document;\n";
	echo "var list = par.getElementById('photo_$id');\n";
	echo "list.src = '$dir/$newname';\n";
	echo "</script>\n";
	echo "</head>\n";
	echo "</html>\n";
    exit();
endif;
?>
<html style = 'background-color: #fffacd'><head>
<script language="javascript">
function upload(){
// hide old iframe
    var par = window.parent.document;
    var num = par.getElementsByTagName('iframe').length - 1;
    var iframe = par.getElementsByTagName('iframe')[num];
    iframe.className = 'hidden';

    // create new iframe
    var new_iframe = par.createElement('iframe');
<?php  echo "new_iframe.src = 'photo_upload.php?id=$id'\n;"; ?>
    new_iframe.frameBorder = '0';
    par.getElementById('iframe').appendChild(new_iframe);

    document.iform.submit();
}
</script>
<style>
body {vertical-align:top;}
</style>
<link rel='stylesheet' href='styles/vbcll.css' type = 'text/css' />
<head>
<body >
<div id = 'corps' style = 'background-color: #fffacd'>
<p>Ajouter une photo<br />(largeur: 100px): 
<form name="iform" action="" method="post" enctype="multipart/form-data">
<input id="file" type="file" name="image" size = '10' onchange="upload()" />
<input type="hidden" name="imgnum" /></p>
</form>
</div>
</body>
</html>
